/*todo  预制体的封装，包含资源的预加载与同步 */

import { } from "../threeex/GLTFLoader";

class GltfMeshFac {
    constructor() {
        this._m_map = {}
    }

    crtByKey(modkey) {
        return this._m_map[modkey].clone();
    }

    preload(cb) {
        this._load_gltflist(cb);
        return this;
    }

    _insert_map(mapkey, obj) {
        if (this._m_map[mapkey]) {
            return;
        }
        this._m_map[mapkey] = obj.scene.children[0];
    }

    _load_gltflist(cb) {
        let loader = new THREE.GLTFLoader();
        let nidx = 0;
        const mapmcnt = Object.keys(MODEL_GLTF_LIST).length;

        //gltf
        for (const model in MODEL_GLTF_LIST) {
            if (MODEL_GLTF_LIST.hasOwnProperty(model)) {
                const element = MODEL_GLTF_LIST[model];
                loader.load(element, (gltf) => {
                    this._insert_map(model, gltf);
                    nidx += 1;
                    if (nidx == mapmcnt) {
                        cb();
                    }
                })
            }
        }
    }

    update() {
    }
}



export default class PrefabMgr {
    constructor(){
        this._meshlist = [];
        this._bodylist = [];

        this._gltf_fac = new GltfMeshFac();
    }

    static getInstance(pworld){
        if (!this._inc){
            this._inc = new PrefabMgr(pworld);
        }
    }

    preload(){
        this._gltf_fac.preload(()=>{

        });
    }

    update(){
        for (let i = 0; i < this._meshlist.length; i++) {
            const element = this._meshlist[i];
            element.position.copy(this._bodylist[i].position);
            element.quaternion.copy(this._bodylist[i].quaternion);
        }
    }
}